vutils/system_allocator: Always return NULL on zero-size realloc#145
Merged
Conversation
`sys_malloc` and `sys_calloc` already short-circuit to NULL when `bytes * times == 0`, but `sys_realloc` was forwarding straight to libc `realloc(ptr, 0)`, whose behavior is implementation-defined (and undefined as of C23). On macOS that returns a valid 1-byte allocation, so the existing zero-size realloc test asserted NULL and failed. Mirror the malloc/calloc guard in `sys_realloc`, also freeing the old buffer to avoid leaking it when the caller shrinks to zero. The arena backend already returns NULL in this case, so vut_allocator_realloc is now consistent across both backends and all platforms.
JeanJPNM
approved these changes
Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sys_mallocandsys_callocalready short-circuit to NULL whenbytes * times == 0, butsys_reallocwas forwarding straight to libcrealloc(ptr, 0), whose behavior is implementation-defined (and undefined as of C23). On macOS that returns a valid 1-byte allocation, so the existing zero-size realloc test asserted NULL and failed.Mirror the malloc/calloc guard in
sys_realloc, also freeing the old buffer to avoid leaking it when the caller shrinks to zero. The arena backend already returns NULL in this case, so vut_allocator_realloc is now consistent across both backends and all platforms.